<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends Controller {
    //TP构造函数
    public function _initialize(){
        //自动加载的类
        new \Org\Admin_autoload();
        //当前页仅管理员以上可以访问
        /*if($this->admin_info['admin_group_id']>2){
            echo "<script>alert('当前页面仅管理员以上可以访问，请您使用管理员账号登录');history.back(-1);</script>";exit;
        }*/
    }
    //用户管理
    public function index(){
        $data['title']='用户管理 会员 - 找金银网';
        if(I('path.2')!=''){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        $Model=D("Member");
        //搜索 关键词
        if(I('post.search')&&I('post.keyword')==''){
            session('user_keyword',null);
        }
        if(session('user_keyword')){
            $keyword=session('user_keyword');
        }
        if(I('post.search')&&I('post.keyword')){
            $keyword=I('post.keyword');
            session('user_keyword',I('post.keyword'));
        }
        //echo $keyword;exit;
        $Model->keyword=$keyword;

        //搜索 分类
        /*if(I('post.pc_id')){
            $Model->pc_id=I('post.pc_id');
        }*/
        $Model->page=$page;
        $Model->perpage=$perpage;
        $user_list=$Model->get_user_list();
        //print_r($user_list);exit;
        $user_total=$Model->get_user_total();
        //echo $user_total;exit;
        $page_total=ceil($user_total/$perpage);
        //echo $page_total;exit;
        //分页数据
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['perpage']=$perpage;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['user_total']=$user_total;
        $pager['page_total']=$page_total;
        //$cat_list=$Model->get_cat_list();
        $this->assign("user_list", $user_list);
        //$this->assign("cat_list", $cat_list);
        $this->assign("pager",$pager);
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function user_add(){
        $data['title']='添加用户 会员 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //添加用户 提交
    public function user_add_post(){
        if($_POST['member_name']!=''&&$_POST['member_passwd']!=''){
            $post=I('post.');
            if(!empty($_FILES['member_avatar']['tmp_name'])){
                //上传图片
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath  =     './data/upload/user/avatar/'; // 设置附件上传根目录
                $upload->savePath  =     ''; // 设置附件上传（子）目录
                $upload->subName='';
                $upload->saveName=md5(time());
                $info   =   $upload->upload();
                if(!$info) {// 上传失败
                    echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
                }
                $member_avatar='/data/upload/user/avatar/'.$info['member_avatar']['savename'];
                $post['member_avatar']=$member_avatar;
            }
            $member_passwd=I('post.member_passwd');
            $post['member_passwd']=md5($member_passwd);
            $post['member_reg_time']=time();
            //print_r($post);exit;
            $Model=D("Member");
            $Model->post=$post;
            if(!$Model->user_add_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>alert('提交成功');window.location.href='/admin/user/user_add';</script>";
        }else{
            echo "<script>alert('请您填写完整后尝试重新提交');history.back(-1);</script>";exit;
        }
    }
    //查看用户
    public function user_view(){
        $data['title']='查看会员 - 找金银网';
        $member_id=I('path.2');
        $Model=D("Member");
        $Company=D("Company");
        $Model->member_id=$member_id;
        $Company->member_id=$member_id;
        $user_info=$Model->get_user_info();
        $company_info=$Company->get_company_info();
        //print_r($user_info);exit;
        $this->assign("data", $data);
        $this->assign("company_info",$company_info);
        $this->assign("user_info", $user_info);
        $this->display();
    }
    //编辑用户
    public function user_edit(){
        $data['title']='编辑用户 会员 - 找金银网';
        $member_id=I('path.2');
        $Model=D("Member");
        $Model->member_id=$member_id;
        $Company=D("Company");
        $Company->member_id=$member_id;
        $user_info=$Model->get_user_info();
        $company_info=$Company->get_company_info();
        //print_r($user_info);exit;
        $this->assign("data", $data);
        $this->assign("user_info", $user_info);
        $this->assign("company_info",$company_info);
        $this->display();
    }
    //
    public function user_edit_post(){
        if($_POST['member_id']!=''){
            $post=I('post.');
            if(!empty($_FILES['member_avatar']['tmp_name'])){
                //上传图片
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath  =     './data/upload/user/avatar/'; // 设置附件上传根目录
                $upload->savePath  =     ''; // 设置附件上传（子）目录
                $upload->subName='';
                $upload->saveName=md5(time());
                $info   =   $upload->upload();
                if(!$info) {// 上传失败
                    echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
                }
                $member_avatar='/data/upload/user/avatar/'.$info['member_avatar']['savename'];
                $post['member_avatar']=$member_avatar;
            }
            if($_POST['new_member_passwd']){
                $member_passwd=I('post.new_member_passwd');
                $post['member_passwd']=md5($member_passwd);
            }
            $Model=D("Member");
            $Model->post=$post;
            if(!$Model->user_edit_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            //同步修改到UC
            $UC=M('Members','uc_','DB_CONFIG');
            $uid=$post['member_id'];
            //echo $uid;exit;
            $data['email']=$post['member_email'];
            $data['password']=$post['member_passwd'];
            $result=$UC->where('uid='.$uid)->save($data);
            if($result === false) {
                echo "<script>alert('更新到UC失败');history.back(-1);</script>";
            }
            echo "<script>alert('提交成功');history.back(-1);</script>"; //此处修改为跳转到列表
        }else{
            echo "<script>alert('请您提交完整后再尝试重新提交');history.back(-1);</script>";exit;
        }
    }
    //公司编辑
    public function company_edit_post(){
        if($_POST['member_id']!=''){
            $post=I('post.');
            $Model=D("Company");
            $Model->post=$post;
            if(!$Model->company_edit_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>alert('提交成功');window.location.href='/admin/user/index';</script>";
        }else{
            echo "<script>alert('请您提交完整后再尝试重新提交');history.back(-1);</script>";exit;
        }
    }
    //图片上传ajax
    public function company_edit_ajax(){
        error_reporting(0);
        $picname = $_FILES['company_license_num_pic']['name'];
        $picsize = $_FILES['company_license_num_pic']['size'];
        $pic_path = APP_URL."data/upload/company/".$picname;
        move_uploaded_file($_FILES['company_license_num_pic']['tmp_name'], $pic_path);
        echo json_encode($picname);
    }
    //更新排序及批量删除
    public function update_user()
    {
        if(I('post.ids')){
            $ids=I('post.ids');
            $Model=D("Member");
            $Model->ids=$ids;
            if(!$Model->delete_user()){
                echo "<script>alert('删除失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";exit;
        }
        /*elseif(I('post.product_ids')){
            $ids=I('post.product_ids');
            $sorts=I('post.sorts');
            $Model=D("Product");
            $Model->ids=$ids;
            $Model->sorts=$sorts;
            if(!$Model->update_product_sort()){
                echo "<script>alert('更新失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";
        }*/
    }
    //
    public function integral(){
        $data['title']='积分管理 会员 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function prestore(){
        $data['title']='预存金额 会员 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function binding(){
        $data['title']='分享绑定 会员 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function label(){
        $data['title']='用户标签 会员 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
}